<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar title="登陆" class="defind-nav-bar">
      <van-icon name="cross" slot="left" @click="$router.go(-1)" />
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 登陆表单 -->
    <van-form @submit="loginFormSubmit" ref="loginFormRef">
      <van-field
       type="number"
       maxlength="11"
       :rules="loginFormRules.mobile"
       v-model="loginForm.mobile"
       name="mobile" placeholder="请输入手机号">
        <i slot="left-icon" class="toutiao icon-tt-shouji"></i>
      </van-field>
      <van-field type="number"
      maxlength="6"
      :rules="loginFormRules.code"
      v-model="loginForm.code"
      left-icon="lock"
      name="code"
      placeholder="请输入验证码" >
        <!-- 自定义图标 -->
        <i slot="left-icon" class="toutiao icon-tt-yanzhengma"></i>
        <!-- 发送验证码按钮  需要给button指定native-type=""button 否则会触发表单提交事件-->
        <template #button>
          <!-- 倒计时组件 time:倒计时秒数 format:要显示的秒数 @finish倒计时结束的回调 -->
          <van-count-down v-if="timeFlag" :time="60000" format="ss 秒" @finish="timeEnd" />
          <van-button v-else class="send-code" size="small" type="primary" native-type="button" @click="getCode">发送验证码</van-button>
        </template>
      </van-field>
      <div style="margin: 16px;">
        <van-button class="login-btn" block type="info" native-type="submit">登陆</van-button>
      </div>
    </van-form>
    <!-- /登陆表单 -->

  </div>
</template>

<script>
import { login, sendCode } from '@/api/user.js'
// import { mapMutation } from '@store/index.js'
export default {
  data () {
    return {
      // 登陆表单数据
      loginForm: {
        mobile: '17630932214',
        code: '246810'
      },

      // 登陆表单校验规则对象   {键:[{}]}
      loginFormRules: {
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/, message: '手机号格式错误' }
        ],
        code: [
          { required: true, message: '验证码不能为空' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]

      },

      // 倒计时flag
      timeFlag: false
    }
  },
  methods: {
    // 获取验证码
    async getCode () {
      // 1. 验证手机号码
      try {
        // 调用vant表单validata方法, 返回值为promise,如果验证失败会报错, 捕获错误即可
        await this.$refs.loginFormRef.validate('mobile')
      } catch (err) {
        // 如果验证失败 直接返回
        return false
      }
      // 验证通过显示倒计时
      this.timeFlag = true
      // 发起请求
      try {
        await sendCode(this.loginForm.mobile)
      } catch (e) {
        if (e.response.status === 429) return this.$toast.fail('服务器繁忙,请稍候再试!')
        this.$toast.fail('网络出现错误!')
        this.timeFlag = false
      }
      this.$toast.success('发送成功,请注意接受')
    },

    // 倒计时结束时的回调
    timeEnd () {
      this.timeFlag = false
    },

    // 登陆验证
    async loginFormSubmit () {
      // 弹出提示框  $toast只能再页面中出现一次 再次调用时上一个提示会被覆盖关闭
      this.$toast.loading({
        message: '登陆中...',
        forbidClick: true,
        duration: 0
      })
      const info = this.loginForm

      // 发起请求
      try {
        // 抛出错误
        const { data: res } = await login(info)
        // 覆盖上一个加载提示框
        this.$toast.success('登陆成功')
        // 存储token
        this.$store.commit('getUserTouken', res.data)
        // 跳转页面
        this.$router.push('/profile')
        // 捕获错误
      } catch (e) {
        console.dir(e)
        if (e.response && e.response.status === 400) return this.$toast.fail('手机号或验证码错误')
        this.$toast.fail('网络错误,请稍候再试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  .login-btn {
  margin: 58px 14px;
  width: 649px;
  height: 88px;
  border: none;
  background-color:#6DB4FB;
  border-radius: 10px;
  }
  .toutiao {
    font-size: 37px;
  }
  .send-code {
    width: 170px;
    height: 46px;
    line-height: 46px;
    padding: 5px 10px;
    background: #EDEDED;
    color: #666;
    border: none;
    border-radius: 23px;
    .van-button__text {
      font-size: 22px !important;
    }
    &:after {
      content: "";
      position: absolute;
      top: -3px;
      left: -20px;
      width: 1px;
      height: 47px;
      background-color: #ccc;
    }
  }
}
</style>
